/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Filter;
import com.hboxs.asl.entity.CourseTrade;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.entity.YiYuanTrade;
import com.hboxs.asl.service.*;
import com.hboxs.asl.util.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.*;

/**
 * Controller - 数据统计
 */
@Controller("adminDataController")
@RequestMapping("/asl-admin/data")
public class DataController extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "courseTradeServiceImpl")
    private CourseTradeService courseTradeService;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "yiYuanTradeServiceImpl")
    private YiYuanTradeService yiYuanTradeService;

    /**
     * 首页
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String add(ModelMap model) {

        // 时间
        Date date = new Date();
        Filter startDateFilter = null;
        Filter endDateFilter = null;

        /*****************  会员 *****************/
        // 增加的会员数量
        Long memberCount = 0l;
        // 时间段内用户数量增加
        LinkedHashMap<Date, Long> memberCountsMap = new LinkedHashMap();
        Set<Long> memberCounts = new HashSet<>();
        for (int i = -30; i <= 0; i++) {
            Date d = DateUtils.getPastDate(i);
            startDateFilter = Filter.geDate("createDate", DateUtils.starOftDate(d));
            endDateFilter = Filter.leDate("createDate", DateUtils.endOfDate(d));
            memberCount = memberService.count(startDateFilter, endDateFilter);
            memberCountsMap.put(d, memberCount);
            memberCounts.add(memberCount);
        }
        model.addAttribute("memberCountsMap", memberCountsMap);
        // 坐标的最高值图表最高值的120%
        model.addAttribute("memberCountMax", Collections.max(memberCounts) * 1.5);


        /*****************  课程 *****************/
        // 增加的课程数量
        Long courseCount = 0l;
        LinkedHashMap<Date, Long> courseCountsMap = new LinkedHashMap();
        Set<Long> courseCounts = new HashSet<>();
        for (int i = -30; i <= 0; i++) {
            Date d = DateUtils.getPastDate(i);

            startDateFilter = Filter.geDate("datePattern", DateUtils.starOftDate(d));
            endDateFilter = Filter.leDate("datePattern", DateUtils.endOfDate(d));
            courseCount = courseService.count(startDateFilter, endDateFilter);
            courseCountsMap.put(d, courseCount);
            courseCounts.add(courseCount);
        }
        model.addAttribute("courseCountsMap", courseCountsMap);
        // 坐标的最高值图表最高值的120%
        model.addAttribute("courseCountMax", Collections.max(courseCounts) * 1.5);

        /*****************  课程(收费/免费) *****************/
        Filter chargeFilter = null;
        Filter noChargeFilter = null;
        Long money = 0l;
        Long noMoney = 0l;
        LinkedHashMap<Date, Long> moneyCountMap = new LinkedHashMap<>();
        Set<Long> moneyCounts = new HashSet<>();
        LinkedHashMap<Date, Long> noMoneyCountMap = new LinkedHashMap<>();
        Set<Long> noMoneyCounts = new HashSet<>();
        for (int i = -30; i <= 0; i++) {
            Date d = DateUtils.getPastDate(i);
            startDateFilter = Filter.geDate("datePattern", DateUtils.starOftDate(d));
            endDateFilter = Filter.leDate("datePattern", DateUtils.endOfDate(d));
            chargeFilter = Filter.eq("charge", true);
            money = courseService.count(startDateFilter, endDateFilter, chargeFilter);
            moneyCountMap.put(d, money);
            moneyCounts.add(money);
            noChargeFilter = Filter.eq("charge", false);
            noMoney = courseService.count(startDateFilter, endDateFilter, noChargeFilter);
            noMoneyCountMap.put(d,noMoney);
            noMoneyCounts.add(noMoney);
        }

        model.addAttribute("moneyCountMap", moneyCountMap);
        model.addAttribute("noMoneyCountMap", noMoneyCountMap);
        // 坐标的最高值图表最高值的120%
        model.addAttribute("courseCountMax", Collections.max(courseCounts) * 1.5);

        /**********************课程/提问/1元查看答案 成交量********************/
        //课程
        Filter courseFilter = null;
        Long courseTradeCounts = 0l;
        LinkedHashMap<Date, Long> courseTradeCountMap = new LinkedHashMap<>();
        Set<Long> courseTradeCountSet = new HashSet<>();
        //易问易答（提问）
        Filter questionFilter = null;
        Long questionCounts = 0l;
        LinkedHashMap<Date, Long> questionCountMap = new LinkedHashMap<>();
        Set<Long> questionCountSet = new HashSet<>();
        //一元查看答案
        Filter yiyuanFilter = null;
        Long yiyuanTradeCounts = 0l;
        LinkedHashMap<Date, Long> yiyuanTradeCountMap = new LinkedHashMap<>();
        Set<Long> yiyuanTradeCountSet = new HashSet<>();
        for (int i = -30; i<= 0; i++) {
            Date d = DateUtils.getPastDate(i);
            startDateFilter = Filter.geDate("createDate", DateUtils.starOftDate(d));
            endDateFilter = Filter.leDate("createDate", DateUtils.endOfDate(d));
            //课程交易
            courseFilter = Filter.eq("paymentStatus", CourseTrade.PaymentStatus.paid);
            courseTradeCounts = courseTradeService.count(startDateFilter, endDateFilter, courseFilter);
            courseCountsMap.put(d, courseTradeCounts);
            courseTradeCountSet.add(courseTradeCounts);
            //易问易答交易（提问）
            questionFilter = Filter.eq("paymentStatus", Question.PaymentStatus.paid);
            questionCounts = questionService.count(startDateFilter, endDateFilter, questionFilter);
            questionCountMap.put(d, questionCounts);
            questionCountSet.add(questionCounts);
            //一元查看答案交易
            yiyuanFilter = Filter.eq("paymentStatus", YiYuanTrade.PaymentStatus.paid);
            yiyuanTradeCounts = yiYuanTradeService.count(startDateFilter, endDateFilter, yiyuanFilter);
            yiyuanTradeCountMap.put(d, yiyuanTradeCounts);
            yiyuanTradeCountSet.add(yiyuanTradeCounts);
        }
        //课程
        model.addAttribute("courseTradeCountMap", courseTradeCountMap);
        //易问易答（提问）
        model.addAttribute("questionCountMap", questionCountMap);
        //一元查看答案
        model.addAttribute("yiyuanTradeCountMap", yiyuanTradeCountMap);
        // 坐标的最高值图表最高值的120%
        model.addAttribute("max", getMax(Collections.max(courseTradeCountSet), Collections.max(questionCountSet), Collections.max(yiyuanTradeCountSet)) * 1.5);

        return "/admin/data/index";
    }


    public Long getMax(Long first, Long second, Long third) {
        return first>second?(first>third?first:second):(second>third?second:third);
    }


}